<?php

namespace app\api\logic;

use app\common\exception\BusinessException;
use app\common\model\VideoCourseware;

class VideoCollection
{
    /**
     * 查询信息
     * @param $param
     * @return array
     */
    public function getInfo($param): array
    {
        // 提取参数
        $id = $param['id'];
        $userId = $param['userId'];

        // 01.查询合集信息
        $where = [['status', '=', 1], ['id', '=', $id]];
        $info = app(\app\common\model\VideoCollection::class)->where($where)->find();
        if (!$info) {
            throw  new BusinessException('视频合集不存在');
        }

        // 02.查询有效期
        $userVideoCollection = app(\app\common\model\UserVideoCollection::class)->getUserValidVideoCollection($userId, $id);
        $info['isPay'] = (bool)$userVideoCollection;

        // 返回
        return [
            'info' => $info,
            'videoList' => $this->getVideoList($id), // 视频列表
            'coursewareList' => $this->getVideoCoursewareList($id), // 视频列表
        ];
    }

    public function getList($param): array
    {
        // 提取参数
        $page = $param['page'] ?? 1;
        $pageSize = $param['pageSize'] ?? 10;
        $keyword = $param['keyword'] ?? '';
        $courseId = $param['course_id'] ?? 0;

        // 01.查询合集信息
        $where = [
            ['status', '=', 1]
        ];
        if ($keyword) {
            $where[] = ['name', 'like', '%' . $keyword . '%'];
        }
        if ($courseId > 0) {
            $where[] = ['course_id', '=', $courseId];
        }

        // 排序方式
        $order = ['id' => 'desc'];

        // 查询数据
        $list = app(\app\common\model\VideoCollection::class)->where($where)->page($page)->limit($pageSize)->order($order)->select();
        $list = $list ? $list->toArray() : [];

        // 返回
        return $list;
    }

    private function getVideoList($collection_id)
    {
        // 查询条件
        $where = [['a.status', '=', 1], ['collection_id', '=', $collection_id]];

        // 输出字段
        $field = 'a.id,a.name,a.cover,a.duration,a.is_free';

        // 查询数据
        $videoList = app(\app\common\model\Video::class)
            ->alias('a')
            ->field($field)
            ->order(['sort' => 'asc', 'id' => 'asc'])
            ->where($where)
            ->select();
        $videoList = $videoList ? $videoList->toArray() : [];

        // 组装视频数据
        foreach ($videoList as $key => $val) {

        }
        return $videoList;
    }

    private function getVideoCoursewareList($collection_id)
    {
        // 查询条件
        $where = [['a.status', '=', 1], ['collection_id', '=', $collection_id]];

        // 输出字段
        $field = 'a.id,a.name,a.url,a.size';

        // 查询数据
        $model = app(VideoCourseware::class);
        $coursewareList = $model
            ->alias('a')
            ->field($field)
            ->where($where)
            ->select();

        // 组装课件数据
        foreach ($coursewareList as $key => $val) {
            $coursewareList[$key]['type'] = $model->getSuffixAttrByValue($val['url']);
            unset($coursewareList[$key]['url']);
        }
        $coursewareList = $coursewareList ? $coursewareList->toArray() : [];

        return $coursewareList;
    }

}